<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Controllers\admin\ClassifiedController;
class HomeController extends Controller
{
    /**
    *	首页展示的方法
    */
    public function getIndex(Request $request){

    	// 获取一级分类的选项
    	$enum1 = DB::table('y_classified_one')
    			-> get();
    	// 获取热销商品top10来做展示
    	$hotsale = DB::table('goods_basis')
    			-> where('status','=',0)
    			-> orderBy('sales','desc')
    			-> limit(10)
    			-> get();
    	// 获取轮播图（只拿三张轮播图）
    	$poster = DB::table('carousel')   			
    			-> get();
    	// 获取广告位图片
    	$adver = DB::table('adver')
    			-> get();
    	// 获取部分所有商品
    	foreach ($enum1 as  $value) {
    		$goods[] = DB::table('goods_basis')
    				-> where('status','=',0)
    				-> where('class1','=',$value['id'])
    				-> orderBy('evaulate','desc')
    				-> limit(10)
    				-> get();
    	}
        // 获取所有商品
        $enumgoods = DB::table('goods_basis')
                    -> where('status',0)
                    -> orderBy('evaulate','desc')
                    -> get();
     
    	// 获得菜单所需要的三维数组
    	$enum = ClassifiedController::select();
        $i = 0;
        $enumm = [];
     
        while ($i <= count($enum)-1) {
            $enumm[] = $enum[$i];
            $i++;
            if($i == 6){
                break;
            }
        }

        return view('home.home.index',['enum1' => $enum1,
    									'enum' => $enumm,
    									'hotsale' => $hotsale,
    									'poster' => $poster,
    									'adver' => $adver,
    									'goods' => $goods,
                                        'enumgoods' => $enumgoods,
                                       
    									]);
    }
    /**
    *	全部商品业面展示
    */
    public function getAllgoods(Request $request){
    	// 获取全部一级分类
    	$classified1 = DB::table('y_classified_one')
    				-> get();
    	// 获取所有商品
    	$allgoods = DB::table('goods_basis')
    				-> where('status',0)
                    -> where('inventory' ,'!=',0 )
    				-> orderBy('evaulate','desc')
  
    				-> get();
        // 处理首页的更多按钮的进入本页面
        $base = '';
        $checkedClass = '';
        if($request -> has('more')){
            $checkedClass = DB::table('y_classified_one')
                            -> where('id',$request -> input('more'))
                            -> first();
            $base = 1;
                            
        }
        $cname = '';
        if($request -> has('twomore')){
            $checkedClass = DB::table('y_classified_two')
                            -> where('id',$request -> input('twomore'))
                            -> first();
            $cname = DB::table('y_classified_one')
                    -> where('id',DB::table('y_classified_two') -> where('id',$request -> input('ff')) -> value('pid'))
                    -> value('classname');
            $base = 2;
        }
        if($checkedClass){
            $nn = $checkedClass['classname'];
            $card = $checkedClass['id'];
            $pid = $checkedClass['pid'];
        }else{
            $nn = '';
            $card = '';
            $pid = '';
        }
     
        if($request -> has('fn')){
            $fn = 'classified'.$request -> input('fn').'(this)';
        }else{
            $fn = 'classified1(this)';
        }
    	return view('home.goods.allgoods',[
    			'class1' => $classified1,
    			'allgoods' => $allgoods,
                'nn' => $nn,
                'cc' => $card,
                'pid' => $pid,
                'fn' => $fn,
                'base' => $base,
                'ff' => $request -> input('ff' , ''),
                'cname' => $cname
    		]);
    }
    /**
    *	全部商品页面接受一级菜单发过来的ajax
    */
    public function getClass1(Request $request){
    	if(!$request -> has('id')){
    		return 0;
    	}
    	// 根据发过来的clas判断数据要查询的库
    	switch ($request -> input('clas')) {
    		case 2: $class = 'y_classified_two';
    		break;
    		case 3: $class = 'y_classified_three';
    		break;
    	}
    	// 通过ajax发过来的数据获取相关二级分类
    	$res = DB::table($class)
    		-> where('pid',$request -> input('id'))
    		-> get();
    	return $res;
    }
    /**
    *	全部商品上层菜单退回myback接受的方法
    */
    public function getClassback(Request $request){
    	if(!$request -> has('pid')){
    		return 0;
    	}
    	// 对传过来的base进行判断
    	switch ($request -> input('base')) {
    		case 1: $base = 'y_classified_one';
    		break;
    		case 2: $base = 'y_classified_two';
    		break;
    	}
    	// 进行数据库查询
    	$res = DB::table($base)
    		-> where('pid',$request -> input('pid'))
    		-> get();
    	
    	return $res;
    }
    /**
    *	全部商品排序
    */
    public function getSort(Request $request){
        $a = '=';
        // 根据传过来的值判断对要查询的字段
        switch ($request -> input('base')) {
            case 'classified1(this)': $class = 'class1';
                break;
            case 'classified2(this)': $class = 'class2';
                break;
            case 'classified3(this)': $class = 'class3';
                break;
            case 4: $class = 'class1';
                    $a = '!=';
                break;
        }
    	// 对新的数据进行排序获取

    	$res = DB::table('goods_basis')
    		-> where($class,$a,$request -> input('card'))
            -> where('status',0)
    		-> orderBy($request -> input('order'),'desc')
    		-> get();
    	return $res;
    }
    /**
    *   商品详情视图
    */
    public function getGoodsdetail($id){
        // 通过id查询商品基础表
        $goodsBasis = DB::table('goods_basis')
                    -> where('id',$id)
                    -> get()[0];
        // 获取商品详情表的信息
        $goodsDetail = DB::table('goods_details')
                    -> where('gid',$id)
                    -> get()[0];
     
        // 按照br分割数组 定义数组属性
        $attri = explode('<br/>', $goodsDetail['attrbute']);
        foreach ($attri as $key => $value) {
            if($value != ':' && !empty($value)){
                $attr[] = explode(':', $value);
            } 
        } 
        // 通过id查询出商品下的评论
        $evaluate = DB::table('evaluate')
                    -> where('gid',$id)
                    -> where('status',0)
                    -> orderBy('ctime','desc')
                    -> get();

     
        $toid = '';
        $to_es = [];
        // 取出回复的id
        foreach ($evaluate as $key => $value) {
            $toid[] = $value['id'];
        }

        if($toid){
            // 获取全部回复的回复
            $to_es = DB::table('to_evaluate')
                    -> where('status',0)
                    -> whereIn('pid',$toid)
                    -> orderBy('id','desc')
                    -> get();
        }
        
        
        return view('home.goods.goodsdetail',[
            'goodsbasis' => $goodsBasis,
            'goodsdetail' => $goodsDetail,
            'attr' => $attr,
            'evaluate' => $evaluate,
            'gid' => $id,
    
            'to_es' => $to_es
            ]);
    }
}
